package com.zzz.service.impl;

import com.aaa.entity.OOrder;
import com.aaa.entity.UComment;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzz.feign.InsertFeign;
import com.zzz.mapper.OOrderMapper;
import com.zzz.service.UCommentService;
import com.zzz.mapper.UCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 86176
 * @description 针对表【u_comment】的数据库操作Service实现
 * @createDate 2023-03-06 15:03:56
 */
@Service
@SuppressWarnings("all")
public class UCommentServiceImpl extends ServiceImpl<UCommentMapper, UComment>
        implements UCommentService {

    @Autowired
    private UCommentMapper uCommentMapper;

    @Autowired
    private InsertFeign insertFeign;

    @Autowired
    private OOrderMapper orderMapper;

    //查询商品所有评论
    @Override
    public Result commentAndGoodById(String goodsid, Integer pageCurrent, Integer pageSize) {
        PageHelper.startPage(pageCurrent, pageSize);
        List<UComment> uComment = uCommentMapper.commentAndGoodById(goodsid);
        for (UComment comment : uComment) {
            String img = comment.getImg();
            if (img != null) {
                String[] split = img.split(",");
                comment.setImgss(split);
            }
        }
        PageInfo<UComment> pageInfo = new PageInfo<>(uComment);
        return new Result(2000, "查询成功", pageInfo);
    }

    //查询商品所有评论下我发表的评论
    @Override
    public Result findMyComment(String goodsid, Integer pageCurrent, Integer pageSize) {
        PageHelper.startPage(pageCurrent, pageSize);
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username1 = tokenChaim.get("username");
        String username = username1.toString();
        List<UComment> myComment = uCommentMapper.findMyComment(goodsid, username);
        for (UComment comment : myComment) {
            String img = comment.getImg();
            if (img != null) {
                String[] split = img.split(",");
                comment.setImgss(split);
            }
        }
        PageInfo<UComment> pageInfo = new PageInfo<>(myComment);
        return new Result(2000, "查询成功", pageInfo);
    }

    //添加评论
    @Override
    public Result addComment(UComment uComment) {
        String[] imgs = uComment.getImgss();
        StringBuffer sb = new StringBuffer();
        for (String str : imgs) {
            sb.append(str).append(",");
        }
        String s = sb.deleteCharAt(sb.length() - 1).toString();
        uComment.setImg(s);
        int insert = uCommentMapper.insert(uComment);
        String goodsid = uComment.getGoodsid();
        String bid = uComment.getBid();
        String userid = uComment.getUserid();
        orderMapper.updateOrder(goodsid,bid,userid);
        return new Result(2000, "添加成功", insert);
    }

    //添加图片
    @Override
    public Result insert(MultipartFile file) {
        Result updateload = insertFeign.updateload(file);
        return updateload;
    }

    //删除评论
    @Override
    public Result delete(String id) {
        uCommentMapper.deleteById(id);
        return new Result<>(2000, "删除成功");
    }

    //查询要修改的评论
    @Override
    public Result getCommentById(String id) {
        UComment uComment = uCommentMapper.selectById(id);
        if (uComment.getImg() != null) {
            String img = uComment.getImg();
            String[] split = img.split(",");
            uComment.setImgss(split);
        }
        return new Result(2000, "查询成功", uComment);
    }

    //修改评论
    @Override
    public Result updateComment(UComment uComment) {
        if (uComment.getImgss() != null) {
            System.out.println(uComment);
            String[] imgs = uComment.getImgss();
            StringBuffer sb = new StringBuffer();
            for (String str : imgs) {
                sb.append(str).append(",");
            }
            String s = sb.toString();
            uComment.setImg(s);
        }
        uComment.setUpdatetime(new Date());
        int i = uCommentMapper.updateById(uComment);
        return new Result(2000, "修改成功");
    }
}




